from django.contrib import admin
from .models import BillOfMaterials, BOMComponent, BOMVersion, AlternativeMaterial, BOMDocument


@admin.register(BillOfMaterials)
class BOMAdmin(admin.ModelAdmin):
    list_display = ('bom_code', 'product', 'current_version', 'bom_type', 'status', 'lifecycle_status', 'effective_date', 'expiry_date')
    list_filter = ('bom_type', 'status', 'lifecycle_status', 'is_active')
    search_fields = ('bom_code', 'product__name', 'product__sku')
    ordering = ('-create_time',)


@admin.register(BOMComponent)
class BOMComponentAdmin(admin.ModelAdmin):
    list_display = ('bom', 'material', 'quantity', 'unit', 'unit_cost', 'total_cost')
    list_filter = ('unit', 'operation_code')
    search_fields = ('bom__bom_code', 'material__name', 'material__sku')
    ordering = ('bom', 'sequence')


@admin.register(BOMVersion)
class BOMVersionAdmin(admin.ModelAdmin):
    list_display = ('bom', 'version_number', 'status', 'is_active', 'effective_date', 'created_by')
    list_filter = ('status', 'is_active')
    search_fields = ('bom__bom_code', 'version_number')
    ordering = ('-create_time',)


@admin.register(AlternativeMaterial)
class AlternativeMaterialAdmin(admin.ModelAdmin):
    list_display = ('bom_component', 'alternative_material', 'is_active', 'approval_status')
    list_filter = ('is_active', 'approval_status')
    search_fields = ('bom_component__material__name', 'alternative_material__name', 'alternative_material__sku')
    ordering = ('-create_time',)


@admin.register(BOMDocument)
class BOMDocumentAdmin(admin.ModelAdmin):
    list_display = ('bom', 'doc_type', 'doc_name', 'upload_time')
    list_filter = ('doc_type',)
    search_fields = ('bom__bom_code', 'doc_name')
    ordering = ('-upload_time',)